#Requires AutoHotkey v2.0

#Include g:\AHK\git-ahk-lib\lib\gdip\GdipStarter.ahk
Gdip_Startup()

CoordMode 'Mouse', 'Screen'
CoordMode 'ToolTip', 'Screen'
CoordMode 'Pixel', 'Screen'

ScreenCapBox(1)

ScreenCapBox(Options := 0) {
  x0 := DllCall("GetSystemMetrics", "Int", 76)
  y0 := DllCall("GetSystemMetrics", "Int", 77)
  Width := DllCall("GetSystemMetrics", "Int", 78)
  Height := DllCall("GetSystemMetrics", "Int", 79)
  TopLeftX := x0
  TopLeftY := y0
  XWidth := Width
  YHeight := Height
  MypBitmap := 0
  movespeed := 1
  bCursor := Options & 1
  ShowStatic := 0

  g := Gui('-Caption +AlwaysOnTop +ToolWindow +E0x00080000')
  g.Show('NA x0 y0')

  pPen := Gdip_CreatePen(0xff00aeff, 2)
  pPenszx := Gdip_CreatePen(0xff00aeff, 1)
  pBrush := Gdip_BrushCreateSolid(0x8f000000)
  pPenbk := Gdip_CreatePen(0xff000000, 1)

  OldMX := 0, OldMY := 0

  while (GetKeyState("LButton", "P") = 0) {
    MouseGetPos(&NewMX, &NewMY, &VarWin)
    WinGetPos(&TopLeftX, &TopLeftY, &XWidth, &YHeight, 'ahk_id' VarWin)
    Rect := Buffer(16, 0)
    DllCall("GetClientRect", "Uint", VarWin, "Ptr", Rect)
    DllCall("ClientToScreen", "Uint", VarWin, "Ptr", Rect)
    tempy := NumGet(Rect, 4, "Uint")
    tempy := tempy > 0x7FFFFFFF ? -~tempy : tempy
    TopLeftX := NumGet(Rect, 0, "Uint")
    TopLeftX := TopLeftX > 0x7FFFFFFF ? -~TopLeftX : TopLeftX
    RightBottomX := TopLeftX + NumGet(Rect, 8, "Uint")
    RightBottomY := tempy + NumGet(Rect, 12, "Uint")
    TopLeftX := TopLeftX < x0 ? x0 : TopLeftX
    TopLeftY := TopLeftY < y0 ? y0 : TopLeftY
    RightBottomX := RightBottomX > x0 + Width ? x0 + Width : RightBottomX
    RightBottomY := RightBottomY > y0 + Height ? y0 + Height : RightBottomY
    XWidth := RightBottomX - TopLeftX
    YHeight := RightBottomY - TopLeftY
    If !((NewMX = OldMX) && (NewMY = OldMY)) {

      Tooltip XWidth 'x' YHeight, TopLeftX, TopLeftY
      OldMX := NewMX
      OldMY := NewMY

      hbm := CreateDIBSection(Width, Height)
      hdc := CreateCompatibleDC()
      obm := SelectObject(hdc, hbm)
      G_ := Gdip_GraphicsFromHDC(hdc)
      Gdip_SetSmoothingMode(G_, 4)
      hbm2 := CreateDIBSection(XWidth, YHeight)
      hdc2 := CreateCompatibleDC()
      obm2 := SelectObject(hdc2, hbm2)
      Gdip_FillRoundedRectangle(G_, pBrush, 0, 0, Width, Height, 1)
      BitBlt(hdc, TopLeftX, TopLeftY, XWidth, YHeight, hdc2, 0, 0)		;透明遮罩
      Gdip_DrawRoundedRectangle(G_, pPen, TopLeftX - 1, TopLeftY - 1, XWidth + 1, YHeight + 1, 0)		;蓝色边框
      SelectObject(hdc2, obm2)
      DeleteObject(hbm2)
      DeleteDC(hdc2)
    }
    UpdateLayeredWindow(g.Hwnd, hdc, X0, Y0, Width, Height)
    SelectObject(hdc, obm)
    DeleteObject(hbm)
    DeleteDC(hdc)
    Gdip_DeleteGraphics(G_)

    If (GetKeyState("RButton", "P") = 1) or (GetKeyState("Esc", "P") = 1) {
      Gdip_DisposeImage(MypBitmap)
      exit
    }
    Else If (GetKeyState("Enter", "P") = 1)
      Break
  }
}